package com.lenovo.smarttraffic.Test.web_load_sql;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.database.dao.DaoManager_Web;
import com.lenovo.smarttraffic.database.dao.SqlBeanDao;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class Web_load_sql_Activity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_test_1;
    private TextView tv_test_1;
    private String TAG = "Web_load_sql_Activity";
    private List<SqlBean> list;
    private Button btn_test_2;
    private SqlBeanDao sqlBeanDao;
    private List<GetCarInfoBean.ROWSDETAILBean> rows_detail;
    private Button btn_test_3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_load_sql_);
        DaoManager_Web.getInstance().init(this);
        initData();
        initView();
    }

    private void initView() {
        sqlBeanDao = DaoManager_Web.getInstance().getDaoSession().getSqlBeanDao();
        btn_test_1 = (Button) findViewById(R.id.btn_test_1);
        tv_test_1 = (TextView) findViewById(R.id.tv_test_1);
        btn_test_1.setOnClickListener(this);
        btn_test_2 = (Button) findViewById(R.id.btn_test_2);
        btn_test_2.setOnClickListener(this);
        btn_test_3 = (Button) findViewById(R.id.btn_test_3);
        btn_test_3.setOnClickListener(this);
    }

    private void initData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.1.110:8088/transportservice/action/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitApi retrofitApi = retrofit.create(RetrofitApi.class);
        Call<GetCarInfoBean> call = retrofitApi.getCall(new RawBean("user1"));


        call.enqueue(new Callback<GetCarInfoBean>() {
            @Override
            public void onResponse(Call<GetCarInfoBean> call, Response<GetCarInfoBean> response) {
                rows_detail = response.body().getROWS_DETAIL();
                Log.e(TAG, "onResponse: " + rows_detail.get(5).toString());
            }

            @Override
            public void onFailure(Call<GetCarInfoBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_test_1:
                list = sqlBeanDao.queryBuilder().list();
                if (list == null) {
                    Toast.makeText(this, "list 为空  ", Toast.LENGTH_SHORT).show();
                } else {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 0; i < list.size(); i++) {
                        stringBuilder.append(list.get(i).getCarbrand());
                        stringBuilder.append(list.get(i).getId());
                        stringBuilder.append(list.get(i).getPcardid());
                        stringBuilder.append(list.get(i).getBuydata());
                        stringBuilder.append(list.get(i).getCarnumber());
                        stringBuilder.append(list.get(i).getNumber());
                        stringBuilder.append("\n");
                    }
                    tv_test_1.setText(stringBuilder);
                }
                Toast.makeText(this, "show   sql   =="+sqlBeanDao.queryBuilder().list().size(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_test_2:
                sqlBeanDao.deleteAll();
                Toast.makeText(this, " insert  加入全部数据删除", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_test_3:
                for (int i = 0; i < rows_detail.size(); i++) {
                    sqlBeanDao.insert(new SqlBean(null, rows_detail.get(i).getCarnumber(), rows_detail.get(i).getNumber(), rows_detail.get(i).getPcardid(), rows_detail.get(i).getBuydata(), rows_detail.get(i).getCarbrand()));
                }
                Toast.makeText(this, "insert   rows_detail"+sqlBeanDao.queryBuilder().list().size(), Toast.LENGTH_SHORT).show();

                break;

        }
    }
}
